package com.core.web.struts2.taglib;

import java.util.*;

import org.apache.commons.logging.*;
import org.apache.struts2.views.jsp.*;

import com.core.cache.*;

/**
 * CheckBox JSP 标签
 * 
 * 
 */
public class CheckboxTag extends CommonTag {

	private Log log = LogFactory.getLog(RadioTag.class);
	String show = "";
	String cachename = "";

	public CheckboxTag() {
	}

	@Override
	public int doStartTag() throws javax.servlet.jsp.JspException {
		return SKIP_BODY;
	}

	@Override
	public int doEndTag() throws javax.servlet.jsp.JspException {
		try {
			pageContext.getOut().print(getHtmlStr());
		} catch (Exception e) {
			throw new javax.servlet.jsp.JspException(e);
		}
		return EVAL_BODY_INCLUDE;
	}

	public String getHtmlStr() {
		String ret = "";

		String strComm = super.getCommonHtml();
		String tmpTag = "<input type=\"checkbox\"" + strComm;

		String code = "";
		Object stackObject = TagUtils.getStack(this.pageContext)
				.findValue(name);

		if (getValue() == null) {
			code = getValue();
		}

		if (stackObject != null) {
			code = stackObject.toString();
		}

		if (cachename != null && !cachename.equals("")) {
			List<Common> datas = CommonCache.getInstance().getCommon(
					this.cachename);
			if (datas != null && !datas.isEmpty()) {
				for (Common data : datas) {
					ret += tmpTag + " value=\"" + data.getKey() + "\" "
							+ (data.getKey().equals(code) ? " checked" : "")
							+ ">" + "&nbsp;" + data.getValue() + "&nbsp;";
				}
			}
		} else {
			tmpTag += " value=\"" + code + "\" >" + "&nbsp;" + show;
			ret = tmpTag;
		}

		return ret;
	}

	public String getCachename() {
		return cachename;
	}

	public void setCachename(String cachename) {
		this.cachename = cachename;
	}

	public String getShow() {
		return show;
	}

	public void setShow(String show) {
		this.show = show;
	}
}
